package com.controller
{
	
	import com.model.valueObjects.VOCountryMaster;
	import com.service.CountryMaster;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	
	
	public class CountryMasterController extends AbstractController
	{
		import com.model.Model;
		
		[Inject]
		public var countryMasterDetails:CountryMaster;
		
		//[PostConstruct]
		public function CountryMasterController()
		{
			super();	
		}
		
		[EventHandler(event="CountryMasterEvent.GET_ALL")]
		public function getAll():void{
			model.currentAdminTableIndex=1;	
			sh.executeServiceCall(countryMasterDetails.getAllCountries(),getAllResult, handleFault );
		}
		private function getAllResult(event:ResultEvent):void
		{
			model.countryMasterList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="CountryMasterEvent.GET_CURRENCY_COUNTRY")]
		public function getAllCurrencyCountry():void{
			sh.executeServiceCall(countryMasterDetails.getCountriesNotHaveCurrency(),getAllCurrencyCountryResult, handleFault );
		}		
		private function getAllCurrencyCountryResult(event:ResultEvent):void
		{
			model.countryCurrencyList=event.result as ArrayCollection;
		}
		
		[EventHandler(event="CountryMasterEvent.ADD", properties="countryMastervo")]
		public function add(countryMastervo:VOCountryMaster):void
		{
			sh.executeServiceCall(countryMasterDetails.addNewCountry(countryMastervo), addResult, handleFault );
		}
		private function addResult(event:ResultEvent):void
		{
			model.countryMasterList=event.result as ArrayCollection;
			
			if(model.saveCloseBtn)model.currentForm=1;

			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("New Country Added","country"  );
		}

		[EventHandler(event="CountryMasterEvent.UPDATE", properties="countryMastervo")]
		public function updateCompany(countryMastervo:VOCountryMaster):void
		{
			sh.executeServiceCall(countryMasterDetails.editCountry(countryMastervo), updateResult, handleFault );	
		}
		private function updateResult(event:ResultEvent):void
		{
			model.countryMasterList=event.result as ArrayCollection;
			
			model.currentForm=1;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("Details Updated ","country" );
		}
		
		[EventHandler(event="CountryMasterEvent.DELETE", properties="serialnumber")]
		public function deleteR(serialnmumber:int):void
		{
			sh.executeServiceCall(countryMasterDetails.deleteCountry(serialnmumber), deleteResult, handleFault );	
		}
		private function deleteResult(event:ResultEvent):void
		{
			model.currentForm=1;
			getAll();
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("Record Deleted ","country" );
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show(event.fault.faultDetail,"Error" );
		}
	}
}